package com.hailin.lease.web.app.controller.login;

import com.hailin.lease.common.login.LoginUserHolder;
import com.hailin.lease.common.result.Result;
import com.hailin.lease.web.app.service.LoginService;
import com.hailin.lease.web.app.vo.user.LoginVo;
import com.hailin.lease.web.app.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author: yuhailin
 * @description 登录请求类
 * @date: 2024/8/4 17:21
 */

@Tag(name = "登录管理")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/app")
public class LoginController {
    private final LoginService loginService;

    /**
     * 获取短信验证码
     * @return
     */
    @RequestMapping("/login/getCode")
    @Operation(summary = "获取短信验证码")
    public Result getCode(@RequestParam String phone) {
        log.info(phone);
        loginService.getCode(phone);
        return Result.ok();
    }

    @PostMapping("login")
    @Operation(summary = "登录")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        log.info(loginVo.getPhone());
        String token = loginService.login(loginVo);
        return Result.ok(token);
    }

    @GetMapping("info")
    @Operation(summary = "获取登录用户信息")
    public Result<UserInfoVo> info() {
        UserInfoVo info = loginService.getUserInfoById(LoginUserHolder.getLoginUser().getUserId());
        return Result.ok(info);
    }
}
